package com.gitee.loyo

import org.h2.jdbcx.JdbcDataSource
import javax.sql.DataSource

open abstract class H2TestCase {
    val dataSource: DataSource by lazy {
        Class.forName("org.h2.Driver")
        JdbcDataSource().apply {
            setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1")
            user = "sa"
            password = "sa"
        }.also { ds ->
            ds.connection.use {
                it.createStatement().use {
                    it.executeUpdate("""
                        CREATE TABLE IF NOT EXISTS t_user(
                            user_id bigint AUTO_INCREMENT NOT NULL primary key,
                            user_name varchar(100) NOT NULL,
                            sex int(1) NOT NULL,
                            active tinyint NOT NULL DEFAULT 1
                        )
                    """.trimIndent())
                    it.executeUpdate("""
                        CREATE TABLE IF NOT EXISTS t_job(
                            employee_id bigint NOT NULL,
                            user_id bigint NOT NULL,
                            title varchar(100) NOT NULL,
                            salary decimal(8,2) NOT NULL,
                            PRIMARY KEY(employee_id, user_id)
                        )
                    """.trimIndent())
                    it.executeUpdate("""
                        CREATE TABLE IF NOT EXISTS t_user_profile(
                            user_id bigint NOT NULL primary key,
                            education varchar(100) NOT NULL
                        )
                    """.trimIndent())
                    it.executeUpdate("""
                        CREATE TABLE IF NOT EXISTS t_daily_log_20210806(
                            log_id bigint AUTO_INCREMENT NOT NULL primary key,
                            log_time timestamp(0) NOT NULL
                        )
                    """.trimIndent())
                }
            }
        }

    }

}